# -*- coding: utf-8 -*-
# @Time : 2021/4/18 15:38
# @Author : husongjiang
# @File : cnn.py

import torch
from torch.utils import data # 获取迭代数据
from torch.autograd import Variable # 获取变量
import torchvision
from torchvision.datasets import mnist # 获取数据集
import matplotlib.pyplot as plt

import gensim
from torch import nn
import numpy as np


VECTOR_DIR = 'D:\model\wordembedding\Tencent_AILab_ChineseEmbedding_Min.txt' # 词向量模型文件

'''
data_path = ''
train_data = mnist.MNIST(data_path,train=True,transform=data_tf,download=False)
test_data = mnist.MNIST(data_path,train=False,transform=data_tf,download=False)

train_loader = data.DataLoader(train_data,batch_size=128,shuffle=True)
test_loader = data.DataLoader(test_data,batch_size=100,shuffle=True)
'''
# 定义网络结构

class TextCNN(nn.Module):
    def __init__(self):
        super(TextCNN, self).__init__()
        self.embedding = nn.Embedding(6210,64)
        self.conv = nn.Conv1d(64,256,5)
        self.f1 = nn.Sequential(nn.Linear(256*596, 128),
                                nn.ReLU())
        self.f2 = nn.Sequential(nn.Linear(128, 2),
                                nn.Softmax())
    def forward(self, x):
        x = self.embedding(x)
        x = x.detach().numpy()
        x = np.transpose(x,[0,2,1])
        x = torch.Tensor(x)
        x = Variable(x)
        x = self.conv(x)
        x = x.view(-1,256*596)
        x = self.f1(x)
        return self.f2(x)

model = TextCNN()
print(model)
